package com.finace.money.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.CameraUtil;
import com.finace.money.common.util.LogUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.di.component.AppComponent;
import com.finace.money.di.component.DaggerUploadImageComponent;
import com.finace.money.di.module.UploadImageModule;
import com.finace.money.presenter.UploadImagePresenter;
import com.finace.money.presenter.contract.UploadImageContract;
import com.finace.money.ui.widget.MyPopupWindow;

import java.io.File;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2018/2/1.
 */

public class UploadImageActivity extends BaseActivity<UploadImagePresenter> implements View.OnClickListener, UploadImageContract.View {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.iv_face)
    ImageView ivFace;
    @BindView(R.id.iv_identity)
    ImageView ivIdentity;
    @BindView(R.id.button_open_account_commit)
    Button buttonOpenAccountCommit;

    private MyPopupWindow mCameraPopupWindow;
    private MyPopupWindow mCommitPopupWindow;
    private final int FACE = 0;
    private final int IDENTITY = 1;
    private int mFace = FACE;
    private File fileFace;
    private File fileIdentity;
    private String phone;
    private String imgcode;
    private String code;
    private String name;
    private String email;
    private String numberPerson;
    private String province;
    private String city;
    private String area;
    private String addressDetail;


    @Override
    public int setLayout() {
        return R.layout.activity_upload_image;
    }

    @Override
    public void init() {
        ivFace.setOnClickListener(this);
        ivIdentity.setOnClickListener(this);
        buttonOpenAccountCommit.setOnClickListener(this);
        tvHeadTitle.setText(UIUtil.getString(R.string.person_upload));
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        imgcode = intent.getStringExtra("imgcode");
        code = intent.getStringExtra("code");
        name = intent.getStringExtra("name");
        email = intent.getStringExtra("email");
        numberPerson = intent.getStringExtra("numberPerson");
        province = intent.getStringExtra("province");
        city = intent.getStringExtra("city");
        area = intent.getStringExtra("area");
        addressDetail = intent.getStringExtra("addressDetail");
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerUploadImageComponent.builder().appComponent(appComponent)
                .uploadImageModule(new UploadImageModule(this))
                .build().inject(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.rl_left:
                onBackPressed();
                break;

            case R.id.button_open_account_commit:
                if(null ==fileFace){
                    UIUtil.showToastSafe(UIUtil.getString(R.string.face_upload));
                    return;
                }
                if(null ==fileIdentity){
                    UIUtil.showToastSafe(UIUtil.getString(R.string.identity_upload));
                    return;
                }
                goCommit();
                break;
            case R.id.button_check_again:
                mCommitPopupWindow.dismiss();
                break;
            case R.id.button_check_sucess:
                mPresenter.register(phone, imgcode, code,
                        name, email, numberPerson,
                        province, city,
                        area, addressDetail, fileFace,
                        fileIdentity);
                mCommitPopupWindow.dismiss();
                break;
            //相机相关
            case R.id.iv_face:
                mFace = FACE;
                chooseImage();
                break;
            case R.id.iv_identity:
                mFace = IDENTITY;
                chooseImage();
                break;
            case R.id.button_camera:
                CameraUtil.openCamera(this);
                dismissPopupWindow();
                break;
            case R.id.button_album:
                CameraUtil.choosePhoto(this);
                dismissPopupWindow();
                break;
        }
    }

    private void goCommit() {

        mCommitPopupWindow = new PopuUtil().initAtLocationPopuWrap(this,
                R.layout.popup_commit, llContent, 265, 115);
        Button button_check_again = mCommitPopupWindow.getContentView().findViewById(R.id.button_check_again);
        Button button_check_sucess = mCommitPopupWindow.getContentView().findViewById(R.id.button_check_sucess);
        button_check_again.setOnClickListener(this);
        button_check_sucess.setOnClickListener(this);
    }

    private void chooseImage() {

        mCameraPopupWindow = new PopuUtil().initAtLocationPopuWrap(this,
                R.layout.popup_camera, llContent, 200, 120);
        Button buttonCamera = mCameraPopupWindow.getContentView().findViewById(R.id.button_camera);
        Button buttonAlbum = mCameraPopupWindow.getContentView().findViewById(R.id.button_album);
        buttonCamera.setOnClickListener(this);
        buttonAlbum.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = null;
        //从相册选取
        if (requestCode == CameraUtil.REQUEST_PICTURE_CHOOSE && resultCode == RESULT_OK) {
            bitmap = CameraUtil.dealPic(this, getImageView(), CameraUtil.REQUEST_PICTURE_CHOOSE, RESULT_OK, data);
        }
        //拍照
        if (requestCode == CameraUtil.REQUEST_CAMERA_IMAGE && resultCode == RESULT_OK
                ) {
            bitmap = CameraUtil.dealPic(this, getImageView(), CameraUtil.REQUEST_CAMERA_IMAGE, RESULT_OK, data);
        }
        if (null != bitmap) {
            if (mFace == FACE) {
                fileFace = CameraUtil.saveBitmapToFile(this, bitmap, "face");
            } else {
                fileIdentity = CameraUtil.saveBitmapToFile(this, bitmap, "identity");
            }
        }

    }

    private ImageView getImageView() {
        switch (mFace) {
            case FACE:
                return ivFace;
            case IDENTITY:
                return ivIdentity;
        }
        return null;
    }

    /**
     * 移除PopupWindow
     */
    public void dismissPopupWindow() {
        if (mCameraPopupWindow != null && mCameraPopupWindow.isShowing()) {
            mCameraPopupWindow.dismiss();
            mCameraPopupWindow = null;
        }
    }

    @Override
    public void commitSucess() {
        dismissPopupWindow();
        Intent intent = new Intent(UIUtil.getContext(), OpenAccountSucessActivity.class);
        intent.putExtra(Constant.email, email);
        startActivity(intent);
        finish();
    }
}
